﻿using System.Linq.Expressions;

namespace Lib.WPF.Core;
/// <summary>
/// 
/// </summary>
public static class PropertyChangedExtension {
    /// <summary>
    /// 
    /// </summary>
    public static string GetPropertyName<T, U>(Expression<Func<T, U>> Exp) {
        if (Exp.Body is MemberExpression MExp) {
            return MExp.Member.Name;
        }
        if (Exp.Body is UnaryExpression UExp) {
            return (UExp.Operand as MemberExpression).Member.Name;
        }
        return default;
    }
    /// <summary>
    /// 
    /// </summary>
    public static void OnPropertyChanged<T, U>(this T That, Expression<Func<T, U>> Exp) where T : PropertyChangedBase, new() {
        That.OnPropertyChanged(GetPropertyName(Exp));
    }
    /// <summary>
    /// 
    /// </summary>
    public static U GetValue<T, U>(this T That, Expression<Func<T, U>> Exp) where T : PropertyChangedBase {
        return That.GetPropertyValue<U>(GetPropertyName(Exp));
    }
    /// <summary>
    /// 
    /// </summary>
    public static void SetValue<T, U>(this T That, Expression<Func<T, U>> Exp, U Value) where T : PropertyChangedBase {
        That.SetPropertyValue<U>(GetPropertyName(Exp), Value);
    }

}